package com.yb.bussiness.tribune;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.tribune.TribuneFlow;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.framework.PageUtil;

public class TribuneFlowServiceImpl implements TribuneFlowService {

	private IBaseDao<TribuneFlow, Long> dao;

	private String FIND_SQL ="SELECT * FROM T_P_TRIBUNE_FLOW WHERE 1=1 ";
	
	public TribuneFlowServiceImpl() {
		dao = new BaseDao<TribuneFlow, Long>(TribuneFlow.class);
	}
	
	public void delTriFlow(Long id) {
		dao.deleteById(id);
	}
	
	public TribuneFlow read(Long id) {
		return dao.read(id);
	}

	public void saveOrUpdate(TribuneFlow trif) {
			dao.saveOrUpdate(trif);
	}

	public List<TribuneFlow> findAll() {
		return dao.findList(FIND_SQL+" ORDER BY LASTVISITTIME");
	}

	public int getCount(Map<String, String> map) {
		try {
			if (map == null || map.size() == 0)
				return 0;
			if (!"".equals(map.get("name")))
				FIND_SQL += "  and name like '%" +map.get("name")+"%'" ;
			if (!"".equals(map.get("typeId")))
				FIND_SQL += "  and typeId = '" +map.get("typeId")+"'" ;
			if (!"".equals(map.get("brandId")))
				FIND_SQL += "  and brandId = '" +map.get("brandId")+"'" ;
			List<TribuneFlow> list = dao.findList(FIND_SQL);
			if (list != null && list.size() > 0)
				return list.size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public List<TribuneFlow> getList(Map<String, String> map, String start,
			String limt) {
		if (map == null || map.size() == 0)
			return null;
		try {
			if (!"".equals(map.get("name")))
				FIND_SQL += "  and name like '%" +map.get("name")+"%'" ;
			if (!"".equals(map.get("typeId")))
				FIND_SQL += "  and typeId = '" +map.get("typeId")+"'" ;
			if (!"".equals(map.get("brandId")))
				FIND_SQL += "  and brandId = '" +map.get("brandId")+"'" ;
			List<TribuneFlow> list = dao.findList(FIND_SQL+" ORDER BY CREATETIME");
			List<TribuneFlow> newlist = null;
			if (list != null && list.size() > 0) {
				newlist = new ArrayList<TribuneFlow>();
				for (TribuneFlow tp : list) {
//					tp.setCreatetimeString(DateTimeUtil.switchDateToString(tp.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
					newlist.add(tp);
				}
			}
			return newlist;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean deleteById(String id) {
		try {
			if ("".equals(id))
				return false;
			String sql = "delete from t_p_TribuneFlow where id=? ";
			dao.delete(sql, new Object[] { id });
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean deleteByIdString(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from t_p_TribuneFlow where id=" + delteid[i];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}
	
	public TribuneFlow getById(String id) {
		if ("".equals(id))
			return null;
		String sql = FIND_SQL+" and id=? ";
		try {
			List<TribuneFlow> list = dao.findList(sql, new Object[] { id });
			if (null != list && list.size() > 0) {
				TribuneFlow tp = list.get(0);
//				tp.setCreatetimeString(DateTimeUtil.switchDateToString(tp.getCreatetime(), "yyyy-MM-dd HH:mm:ss"));
//				tp.setOverTimeString(DateTimeUtil.switchDateToString(tp.getOverTime(), "yyyy-MM-dd HH:mm:ss"));
				return tp;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<TribuneFlow> find(int i, int j) {
		FIND_SQL+=" ORDER BY lastVisitTime limit ?,?";
		try {
			List<TribuneFlow> list = dao.findList(FIND_SQL, new Object[] { i,j });
			List<TribuneFlow> newlist = null;
			if (list != null && list.size() > 0) {
				newlist = new ArrayList<TribuneFlow>();
				for (TribuneFlow tp : list) {
					tp.setCreateTimeString(DateTimeUtil.switchDateToString(tp.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
					tp.setLastVisitTimeString(DateTimeUtil.switchDateToString(tp.getLastVisitTime(), "yyyy-MM-dd HH:mm:ss"));
					newlist.add(tp);
				}
			}
			return newlist;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Map<String, Object> loadTie(String type, int start, int limit) {
		Map<String, Object> map = new HashMap<String, Object>();
		String sql = "select count(b.id) from t_p_TribuneFlow b ";
		if("0".equals(type))
			sql+=" order by lastVisitTime desc";
		if("1".equals(type))
			sql+=" order by createTime desc";
		if("2".equals(type))
			sql+=" order by viewCount desc";
		if("3".equals(type))
			sql+=" order by flowCount desc";
		if("4".equals(type))
			sql+=" where isMarrow ='1' order by lastVisitTime desc";
		int total = Integer.parseInt((dao.findObjectArryList(sql ).get(0))[0].toString());
		map.put("rowCount", total <= limit ? 1 : total / limit == total / limit
				* limit ? total / limit : (total / limit + 1));
		sql = "select * from T_P_TRIBUNEFLOW b ";
		if("0".equals(type))
			sql+=" order by lastVisitTime desc";
		if("1".equals(type))
			sql+=" order by createTime desc";
		if("2".equals(type))
			sql+=" order by viewCount desc";
		if("3".equals(type))
			sql+=" order by flowCount desc";
		if("4".equals(type))
			sql+=" where isMarrow ='1' order by lastVisitTime desc";
		sql+=" limit ?,?";
//		sql = "select b.* from t_p_building b , t_m_proinfo p where b.id=p.proid and p.authorId=? and p.operateType=?  limit "
//				+ start + "," + limit;
		List<TribuneFlow> list = dao.findList(sql, new Object[] { start, limit });
		if (list.size() > 0)
		map.put("flowTieList", list);
		return map;
	}

	public PageUtil getListById(String id ,int start , int limit) {
		PageUtil pu = new PageUtil();
		String sql = "select count(b.id) from t_p_tribune_flow b where tid = ?";
		try {
			int total = Integer.parseInt((dao.findObjectArryList(sql,new Object[] { id }).get(0))[0].toString());
			pu.setTotalRecords(total);
		} catch (NumberFormatException e) {
			pu.setTotalRecords(0);
		}
		FIND_SQL += " and tid = ? limit ?,?";
		List<TribuneFlow> list = dao.findList(FIND_SQL, new Object[] { id, (start-1)*limit, limit });
		pu.setList(list);
		pu.setPageNo(start);
		pu.setPageSize(limit);
		return pu;
	}

	public boolean deleteFlowerTie(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from t_p_tribune_flow where id=" + delteid[i];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

}
